unit TestAVTypes;

interface
uses TestFramework, AVTypes;

type

  TBitArrayTests = class(TTestCase)
  strict private
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure ByteCountTest;
    procedure GetSetBitTest;
    procedure GetSetDataTest;
    procedure ToStringTest;
    procedure AsStringTest;
    procedure SetCountTest;
  end;

  TPropertiesTests = class(TTestCase)
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure GetSetPropertyTest;
  end;

implementation

//TBitArrayTests

  procedure TBitArrayTests.SetUp;
  begin

  end;

  procedure TBitArrayTests.TearDown;
  begin

  end;

  procedure TBitArrayTests.ByteCountTest;
  var ba: TBitArray;
  begin
    Assert(ba.ByteCount = 0, 'Initial size not zero. #1');
    ba[0] := true;
    Assert(ba.ByteCount = 1, 'Size failed. #2');
    ba[8] := true;
    Assert(ba.ByteCount = 2, 'Size failed. #3');
    ba[100*8] := true;
    Assert(ba.ByteCount = 101, 'Size failed. #4');
    ba.Clear;
    Assert(ba.ByteCount = 0, 'Size failed. #5');
    ba.ByteBit[5,2] := false;
    Assert(ba.ByteCount = 6, 'Size failed. #6');
  end;

  procedure TBitArrayTests.GetSetBitTest;
  var ba: TBitArray;
  begin
    ba.ByteBit[5, 3] := true;
    Assert(ba.byte[5] = 8, 'Failed #1');
    ba.byte[5] := 16;
    Assert(ba.ByteBit[5, 4], 'Failed #2');
    Assert(not ba.ByteBit[5, 3], 'Failed #3');
  end;

  procedure TBitArrayTests.GetSetDataTest;
  var ba: TBitArray;
      n: Int64;
  begin
    n := 8;
    ba.SetData(n, SizeOf(n));
    Assert(ba.byte[0] = 8, 'Failed #1');
    Assert(ba.ByteCount = SizeOf(n), 'Failed #2');
    ba.byte[0] := 147;
    ba.GetData(n, SizeOf(n));
    Assert(n = 147, 'Failed #3');
  end;

  procedure TBitArrayTests.ToStringTest;
  var ba: TBitArray;
  begin
    ba.byte[0] := 15;
    Assert(ba.AsString = '0F', 'failed #1');
    ba.byte[0] := 240;
    Assert(ba.AsString = 'F0', 'failed #2');
    ba.byte[0] := 255;
    Assert(ba.AsString = 'FF', 'failed #3');
    ba.byte[1] := 15;
    Assert(ba.AsString = 'FF0F', 'failed #4');
    ba.byte[2] := 115;
    Assert(ba.AsString = 'FF0F73', 'failed #5');
  end;

  procedure TBitArrayTests.AsStringTest;
  var ba: TBitArray;
      s: string;
  begin
    s := '12DF8C7A403AEDFF';
    ba.AsString := s;
    Assert(ba.AsString = s, 'failed #1');
  end;

  procedure TBitArrayTests.SetCountTest;
  var ba: TBitArray;
  begin
    ba.byte[0] := 111;
    ba.byte[1] := 111;
    Assert(ba.ByteCount = 2, 'failed #1');
    ba.ByteCount := 5;
    Assert(ba.ByteCount = 5, 'failed #2');
    Assert(ba.byte[2]+ba.byte[3]+ba.byte[4] = 0, 'failed #3');
    ba.ByteCount := 2;
    Assert(ba.ByteCount = 2, 'failed #4');
    Assert(ba.byte[0] = 111, 'failed #5');
    Assert(ba.byte[1] = 111, 'failed #6');
  end;

//TPropertiesTests

  procedure TPropertiesTests.SetUp;
  begin

  end;

  procedure TPropertiesTests.TearDown;
  begin

  end;

  procedure TPropertiesTests.GetSetPropertyTest;
  var prop: TAVProperties;
      s: string;
  begin
    prop['test'].AsString := 'test value';
    s := prop['test'].AsString;
    Assert(s = 'test value', 'failed #1');
  end;

initialization
  RegisterTest(TBitArrayTests.Suite);
  RegisterTest(TPropertiesTests.Suite);

end.
